<?php

/**
 * @author NNDUC
 * @copyright 2012
 */

include_once (SYSTEM . '/Controller.php');
include_once (MODEL . '/OfferRepository.php');
include_once (APPLICATION .'/Common/Paging.php');

class OfferController extends Controller
{                   
    public function ListOffer() 
    {
    	//include_once (MODEL . '/OfferModel.php');
    	
    	$this->ViewData['Title'] = 'Offer';
    	$offer = new OfferRepository();
        
    	$params = $this->GetParams();
    	$currentCategory = isset($params['category']) ? $params['category'] : '4faa9b416803fa1b05000000';            
        $currentPage = isset($params['page']) ? $params['page'] : 1;       
        $inactivePageTag = 'id="currentPage"';                		
		$pageUrl = '/Offer/ListOffer?category=' . $currentCategory . '&page=';
		$pageUrllast = '';
        $paging =  new Paging();       
        $paging->PageSet($pageUrl, $offer->TotalByCategory($currentCategory), $currentPage, $inactivePageTag, $pageUrllast);        
        $model['result'] = $offer->ViewListOfferByCategory($currentCategory, $paging->start, $paging->perPage);               
        $model['paging'] = $paging;
        
        $this->ViewData['listCat'] = $offer->GetListCategory();
                
        $this->Render('ListOffer', '_Master', $model);
    }
    
    public function CreateOffer() 
    {
    	$this->ViewData['Title'] = 'Create Offer';
		$params = $this->GetParams();
		$offer = new OfferRepository();
		    	
    	if(!isset($params['submit'])) 
    	{
    		$this->ViewData['listCat'] = $offer->GetListCategory(); 
    		$this->Render('CreateOffer', '_Master');	
    	}
    	else 
    	{
	    	include_once (MODEL . '/CreateNewModel.php');	    		    
	    	
	    	$createModel = new CreateNewModel($params);
	    	$message = $createModel->ValidateFields();
	    	if(empty($message))
	    	{	    			    			    	
	    		if(empty($createModel->Image['name'])) 
	    		{
	    			$createModel->Image['name'] = 'Gift.jpg';
	    			$offer->CreateOffer($createModel);
	    			
	    			$this->Redirect('/Offer/ListOffer');	    			
	    		}
	    		else 
	    		{
	    			include_once(ROOT.'/Config.php');
	    			$extension = array('image/jpeg', 'image/png', 'image/gif', 'image/bmp');
	    			$dest = UPLOAD.'/Offer/';	    			
	    			if(in_array($createModel->Image['type'], $extension) && $createModel->Image['size'] < WebConfig::$AppSetting['LimitSize'] * 1024 * 1024) 
	    			{	    				
	    				$createModel->Image['name'] = time() . $createModel->Image['name'];
	    				$dest .= $createModel->Image['name'];	    				
	    				move_uploaded_file($createModel->Image['tmp_name'], $dest);
	    				$offer->CreateOffer($createModel);
	    				
	    				$this->Redirect('/Offer/ListOffer');
	    			}	
	    		}
	    	}
	    	else 
	    	{
	    		$this->Render('CreateOffer', '_Master');	
	    	} 
    	}
    }
    public function DeleteOffer()
    {
        
        $params = $this->GetParams();
        $offer = new OfferRepository();
        //if(isset($params['submit']))
        {
            $filter = array('_id'=> new MongoId($params['offerID']) );
            $offer->DeleteOffer($filter);
        }
        //$this->Redirect('/Offer/ListOffer');
    }
    public function OfferDetail()
    {
        $this->ViewData['Title'] = 'Offer Detail';
        $params = $this->GetParams();
        $offer = new OfferRepository();
        $offerID = $params['offerID'];
        $model['result'] = $offer->GetDetail($offerID);
        $this->ViewData['listCat'] = $offer->GetListCategory();
        $this->Render('OfferDetail','_Master',$model);
        
    }
     public function EditOffer()
    {
        $this->ViewData['Title'] = 'Edit Offer';
		$params = $this->GetParams();
		$offer = new OfferRepository();
	
    	if(isset($params['submit'])) 
    	{
    		
            $offerID = $params['offerID'];
            $model['result'] = $offer->GetDetail($offerID);
            $this->ViewData['listCat'] = $offer->GetListCategory(); 
            
            $this->Render('EditOffer', '_Master',$model);
    	}
    	if(isset($params['edit'])) 
    	{
                    $offLocation = $params['Location'];
                    $offID = $params['offerID'];
	    			$offName = $params['Name'];
	    			$offCategory = $params['Category'];
	    			$offDescription =$params['Description'];
	    			if ((($_FILES["file"]["type"] == "image/gif")
	    					|| ($_FILES["file"]["type"] == "image/jpeg")
	    					|| ($_FILES["file"]["type"] == "image/pjpeg"))
	    					&& ($_FILES["file"]["size"] < 1000000))
	    			{
	    					
	    				if ($_FILES["file"]["error"] > 0)
	    				{
	    					$this->ViewData['error']= "Error: " . $_FILES["file"]["error"] . "<br />";
	    					$this->Render('editImage','_Master');
	    				}
	    				else{
	    					$existFile = UPLOAD."/Offer/".$offID.".jpg";
	    					if(file_exists($existFile))
	    					{
	    						unlink($existFile);
	    					}
	    					move_uploaded_file($_FILES["file"]["tmp_name"],$existFile);
	    					
	    				}
	    			}
	    			else
	    			{
	    				$this->ViewData['error']= "khong dung dinh dang file hoac dung luong qua lon";
	    				$model['result'] = $offer->GetDetail($offID);
	    				$this->ViewData['listCat'] = $offer->GetListCategory();
	    				
	    				$this->Render('EditOffer', '_Master',$model);
	    			}
	    			
	    			$offer->EditOffer($offID,$offName,$offCategory,$offDescription,$offLocation);	    				
	    			$this->Redirect('/Offer/ListOffer');		    		
	    	  	
    	}
    }
            
}